package com.example.grpcapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.grpcapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        var port = "50051"
        if (intent.hasExtra("PORT")) {
            port = intent.getStringExtra("PORT") ?: port
        }
        if (startServer(port)) {
            binding.sampleText.text = "Server started with port $port"
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        stopServer()
    }

    /**
     * A native method that is implemented by the 'grpcapp' native library,
     * which is packaged with this application.
     */
    external fun startServer(port: String): Boolean
    external fun stopServer()

    companion object {
        // Used to load the 'grpcapp' library on application startup.
        init {
            System.loadLibrary("grpcapp")
        }
    }
}